/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author tho.hoang
 */
public abstract class ADetailItem extends AItem {

    public final static int ALIGN_LEFT = 1;
    public final static int ALIGN_RIGHT = 2;
    public final static int ALIGN_CENTER = 3;
    protected Style titleStyle, valueStyle, style;
    protected String title, value;
    protected String[] titleRows = null, valueRow = null;

    public ADetailItem(String id, String title, String value) {
        this.id = id;
        this.title = title;
        this.value = value;
    }

    public ADetailItem(String id, int x, int y, int width, int height, boolean focus, boolean visible, String title, String value) {
        super(id, x, y, width, height, focus, visible);
        this.title = title;
        this.value = value;
    }

    public abstract void paint(int x, int y, Graphics g);

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setCoordinate(int x, int y) {
        setX(x);
        setY(y);
    }

    public void setParent(ACanvas parent) {
        super.setParent(parent);
        if (parent != null) {
            this.style = parent.getStyle().getDetailStyle();
            this.titleStyle = parent.getStyle().getTextStyle();
            this.valueStyle = parent.getStyle().getValueStyle();
        }
    }

    protected void initItem() {
        setTitleRows(title);
        setValueRows(value);
        int h = titleStyle.getHeight() + valueStyle.getHeight();
        if (h > 0) {
            h += style.getPadding().getTopAndBottom();
        }
        this.style.setHeight(h);
        this.height = h;
    }

    public boolean isPaint(int x, int y) {
        if (!visible) {
            return true;
        }
        int itemX = parent.getItemX(), itemY = parent.getItemY(), parentW = parent.getWidth(), parentH = parent.getHeight() - parent.getFooterHeight();
        if (x < itemX || x > parentW) {
            return false;
        }

        if (y + height < itemY || y > parentH) {
            return false;
        }

        return true;
    }

    public boolean isSelectedPaint(int x, int y, int paintHeight) {
        if (!visible) {
            return true;
        }
        int itemX = parent.getItemX(), itemY = parent.getItemY(), parentW = parent.getWidth(), parentH = parent.getHeight() - parent.getFooterHeight();
        if (x < itemX || x + width > parentW) {
            return false;
        }

        if (y < itemY || y + height > parentH) {
            return false;
        }

        return true;
    }

    public int getHeight() {
        initItem();
        return height;
    }

    protected void setTitleRows(String title) {
        this.title = title;
        if (title.length() > 0) {
            this.titleStyle.setWidth(width - style.getPadding().getLeftAndRight());
//            this.titleRows = boldFont.splitWith(title, titleStyle.getWidth() - titleStyle.getPadding().getLeftAndRight());
            this.titleRows = new String[]{title};
            this.titleStyle.setHeight(titleRows.length * font.getFontBold().getHeight() + (titleRows.length - 1) * titleStyle.getRowHeight() + titleStyle.getPadding().getTopAndBottom());
        } else {
            this.titleRows = new String[]{""};
            this.titleStyle.setHeight(0);
            this.titleStyle.setWidth(0);
        }
    }

    protected void setValueRows(String value) {
        this.value = value;
        if (value.length() > 0) {
            this.valueStyle.setWidth(width - style.getPadding().getLeftAndRight());
//            this.valueRow = normalFont.splitWith(value, valueStyle.getWidth() - valueStyle.getPadding().getLeftAndRight());
            this.valueRow = new String[]{title};
            this.valueStyle.setHeight(valueRow.length * font.getFontNormal().getHeight() + (valueRow.length - 1) * valueStyle.getRowHeight() + valueStyle.getPadding().getTopAndBottom());
        } else {
            this.valueRow = new String[]{""};
            this.valueStyle.setHeight(0);
            this.valueStyle.setWidth(0);
        }
    }
}
